﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JilyGameFramework.Classes;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;

namespace JilyGameFramework.Manager
{
    public class InputManager
    {
        private ScreenManager screenManager;

        public MouseState MouseState { get; private set; }
        public MouseState PreviousMouseState { get; private set; }

        public KeyboardState KeyboardState { get; private set; }
        public KeyboardState PreviousKeyboardState { get; private set; }

        public Vector2 Cursor { get; private set; }

        public bool IsPress { get; private set; }

        public event EventHandler<MouseDataArgs> OnMouseAction;

        public InputManager(ScreenManager manager)
        {
            this.screenManager = manager;

            this.KeyboardState = new KeyboardState();
            this.MouseState = new MouseState();

            this.PreviousKeyboardState = new KeyboardState();
            this.PreviousMouseState = new MouseState();
        }

        public void LoadContent()
        {

        }

        public void Update(GameTime gameTime)
        {
            PreviousKeyboardState = KeyboardState;
            PreviousMouseState = MouseState;

            KeyboardState = Keyboard.GetState();
            MouseState = Mouse.GetState();

            IsPress = false;
            if (MouseState.LeftButton == ButtonState.Pressed)
            {
                Cursor = new Vector2(MouseState.X, MouseState.Y);
                IsPress = true;
            }
            else
            {
                var mousepoint = GetFristTouchPoint();
                if (mousepoint != null)
                {
                    Cursor = mousepoint.Value;
                    IsPress = true;
                }
            }

            if (IsPress)
            {
                if (PreviousMouseState.LeftButton == ButtonState.Released)
                {
                    OnMouseActioning(MouseDataArgs.Creater(MouseTpye.Down, Cursor));
                }
                else
                {
                    if (PreviousMouseState.X != MouseState.X || PreviousMouseState.Y != MouseState.Y)
                    {
                        OnMouseActioning(MouseDataArgs.Creater(MouseTpye.Move, Cursor));
                    }
                }
            }
            else
            {
                if (PreviousMouseState.LeftButton == ButtonState.Pressed)
                {
                    OnMouseActioning(MouseDataArgs.Creater(MouseTpye.Up, Cursor));
                }
            }
        }

        public void Draw(GameTime gameTime)
        {

        }

        public Vector2? GetFristTouchPoint()
        {
            TouchCollection touchLocations = TouchPanel.GetState();
            if (touchLocations.Count > 0)
            {
                var point = touchLocations[0].Position;
                MouseState = new MouseState((int)point.X, (int)point.Y, 0, ButtonState.Pressed, ButtonState.Released, ButtonState.Released, ButtonState.Released, ButtonState.Released);
                return point;
            }
            return null;
        }

        protected virtual void OnMouseActioning(MouseDataArgs args)
        {
            if (this.OnMouseAction != null)
            {
                this.OnMouseAction(this, args);
            }
        }


    }
}
